﻿using System;

namespace Quail
{
    public static class PreCreatedObjectFactory
    {
        /// <summary>
        /// Inserts a new pre-created object key
        /// </summary>
        /// <param name="testEnvironmentId">The test testEnvironmentId</param>
        /// <param name="objectType">The type of the object (50 characters max)</param>
        /// <param name="objectKey">The key to the object (500 characters max)</param>
        public static void InsertPreCreatedObject(int testEnvironmentId, string objectType, string objectKey)
        {
            if (string.IsNullOrEmpty(objectType) || objectType.Length > 50)
                throw new ArgumentOutOfRangeException("objectType",
                                                      "Object Type must be defined and 50 or fewer characters long");

            if (string.IsNullOrEmpty(objectKey) || objectKey.Length > 500)
                throw new ArgumentOutOfRangeException("objectKey",
                                                      "Object Key must be defined and 500 or fewer characters long");
            using (var c = QuailServices.QuailDataServiceFactory.CreateClient())
                c.InsertPreCreatedObject(testEnvironmentId, objectType, objectKey);
        }

        /// <summary>
        /// Returns a key from the pre-created object cache
        /// </summary>
        /// <param name="testEnvironmentId">The test testEnvironmentId</param>
        /// <param name="objectType">The type of the object</param>
        /// <returns>A key, or NULL if no pre-created objects are available</returns>
        public static string GetPreCreatedObject(int testEnvironmentId, string objectType)
        {
            using (var c = QuailServices.QuailDataServiceFactory.CreateClient())
                return c.GetPreCreatedObject(testEnvironmentId, objectType);
        }

        /// <summary>
        /// Returns a key from the pre-created object cache
        /// </summary>
        /// <param name="testEnvironmentId">The test testEnvironmentId</param>
        /// <param name="objectType">The type of the object</param>
        /// <returns>A key, or NULL if no pre-created objects are available</returns>
        public static int GetPreCreatedObjectCount(int testEnvironmentId, string objectType)
        {
            using (var c = QuailServices.QuailDataServiceFactory.CreateClient())
                return c.GetPreCreatedObjectCount(testEnvironmentId, objectType);
        }

        /// <summary>
        /// Returns a key from the pre-created object cache
        /// </summary>
        /// <param name="testEnvironmentId">The test testEnvironmentId</param>
        /// <param name="objectType">The type of the object</param>
        /// <returns>A key, or NULL if no pre-created objects are available</returns>
        public static void ClearPreCreatedObjects(int testEnvironmentId, string objectType)
        {
            using (var c = QuailServices.QuailDataServiceFactory.CreateClient())
                c.ClearPreCreatedObjects(testEnvironmentId, objectType);
        }
    }
}
